<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-if="formThead.id" v-model="listQuery.id" style="width: 200px;" class="filter-item" placeholder="id" clearable @keyup.enter.native="handleFilter"/>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <addPage @getLists="getLists"/>
    </div>

    <div class="filter-container">
      <el-checkbox v-model="formThead.id" label="id">id</el-checkbox>
      <el-checkbox v-model="formThead.name" label="用户名称">用户名称</el-checkbox>
      <el-checkbox v-model="formThead.img_url" label="头像路径">头像路径</el-checkbox>
    </div>

    <el-table v-loading="listLoading" :key="tableKey" :data="list" border fit highlight-current-row style="width: 100%;">
      <el-table-column v-if="formThead.id" label="id" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="formThead.name" label="用户名称" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="formThead.img_url" label="头像路径" align="center">
        <template slot-scope="scope">
          <span><img :src="scope.row.img_url" alt="" height="120" @click="bigImage(scope.row.img_url)"></span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--编辑-->
          <editPage :row_data="scope.row" @getLists="getLists"/>
          <!--删除-->
          <el-button type="danger" icon="el-icon-delete" circle @click="del(scope.row.id)"/>
        </template>
      </el-table-column>
    </el-table>
    <!--大图弹窗-->
    <transition name="el-zoom-in-top">
      <div v-show="bigImageSrc !== null && bigImageSrc!==''" class="bigImg" @click="bigImage(null)">
        <img :src="bigImageSrc" style="width:auto; height:400px;border-radius:5px;">
      </div>
    </transition>
    <!--页码-->
    <div class="pagination-container">
      <el-pagination :current-page="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" :total="total" background layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange" @current-change="handleCurrentChange"/>
    </div>
  </div>
</template>

<script>
import { getList, del } from '@/api/thk/vusers'
import store from '@/store'
import waves from '@/directive/waves' // 水波纹指令
import addPage from './addPage'
import editPage from './editPage'
import { moveTo } from '@/api/shop_admin_auth/adminPage'
import { getPage } from '@/api/shop_admin_auth/adminPage'

export default {
  name: 'ComplexTable',
  directives: {
    waves
  },
  components: { addPage, editPage },

  data() {
    return {
      tableKey: 0,
      list: null,
      total: null,
      listLoading: true,
      bigImageSrc: null,
      listQuery: {
        id: undefined,
        img_url: undefined,
        page: 1,
        limit: 20
      },
      formThead: {
        id: true,
        name:true,
        img_url: true
      }
    }
  },
  created() {
    this.getLists()
    // getPage(store.getters.token).then(response => {
    //     // this.data = response.data.data
    //     // this.listLoading = false
    //   })
      // this.son = draggingNode
      // this.parent = dropNode
  },
  methods: {
    getLists() {
      this.listLoading = true
      console.log("123")
      moveTo(store.getters.token, { son_id: 325, parent_id: 0 }).then(res => {
        if (res.data.status) {
          // this.$message.success(res.data.msg)
          // this.$emit('getLists')
          // this.close()
        } else {
          this.$message.error(res.data.msg)
        }
      })
      getList(store.getters.token, this.listQuery).then(res => {
        if (res.data.status) {
          this.list = res.data.list.data
          this.total = res.data.list.total
        } else {
          this.$message.error(res.data.msg)
        }
        this.listLoading = false
      })
    },
    bigImage(src) {
      if (src !== undefined) {
        this.$set(this, 'bigImageSrc', src)
      } else {
        this.$set(this, 'bigImageSrc', null)
      }
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getLists()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getLists()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getLists()
    },
    del(id) {
      this.$confirm('此操作将永久删除该虚拟用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message.info('正在删除，ID：' + id)
        del(store.getters.token, { id: id }).then(res => {
          if (res.status) {
            this.$message.success('已经删除，ID：' + id)
            this.getLists()
          } else {
            this.$message.error('删除失败，ID：' + id)
          }
        })
      }).catch(() => {
        this.$message.info('已取消删除，ID：' + id)
      })
    }
  }
}
</script>
<style scoped>
  .bigImg{
    min-width:100%;
    min-height:100%;
    position:fixed;
    top:0;
    left:0;
    z-index:999999;
    background:rgba(0,0,0,.5);
    display:flex;
    justify-content:center;
    align-items:center;
    cursor:pointer;
  }
  .bigImg img{
    max-width: 600px;
    min-width: 150px;
    height:auto;
    border-radius:10px;
  }
</style>